package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbStorageBo;
import com.ruoyi.tb.lmt.domain.vo.TbStorageVo;
import com.ruoyi.tb.lmt.domain.TbStorage;
import com.ruoyi.tb.lmt.mapper.TbStorageMapper;
import com.ruoyi.tb.lmt.service.ITbStorageService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 出入库申请Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-07
 */
@RequiredArgsConstructor
@Service
public class TbStorageServiceImpl implements ITbStorageService {

    private final TbStorageMapper baseMapper;

    /**
     * 查询出入库申请
     */
    @Override
    public TbStorageVo queryById(Long ID){
        return baseMapper.selectVoById(ID);
    }

    /**
     * 查询出入库申请列表
     */
    @Override
    public TableDataInfo<TbStorageVo> queryPageList(TbStorageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbStorage> lqw = buildQueryWrapper(bo);
        lqw.isNotNull(TbStorage::getLmtNumber);
        lqw.ne(TbStorage::getLmtNumber,"");
            Page<TbStorageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询出入库申请列表
     */
    @Override
    public List<TbStorageVo> queryList(TbStorageBo bo) {
        LambdaQueryWrapper<TbStorage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbStorage> buildQueryWrapper(TbStorageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbStorage> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getLmtNumber()), TbStorage::getLmtNumber, bo.getLmtNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkGroup()), TbStorage::getWorkGroup, bo.getWorkGroup());
        lqw.eq(bo.getWorkGroupId() != null, TbStorage::getWorkGroupId, bo.getWorkGroupId());
        lqw.eq(StringUtils.isNotBlank(bo.getEquipmentCode()), TbStorage::getEquipmentCode, bo.getEquipmentCode());
        lqw.like(StringUtils.isNotBlank(bo.getEquipmentName()), TbStorage::getEquipmentName, bo.getEquipmentName());
        lqw.eq(StringUtils.isNotBlank(bo.getLmtLength()), TbStorage::getLmtLength, bo.getLmtLength());
        lqw.eq(StringUtils.isNotBlank(bo.getAssetsCode()), TbStorage::getAssetsCode, bo.getAssetsCode());
        lqw.eq(StringUtils.isNotBlank(bo.getCodeNumber()), TbStorage::getCodeNumber, bo.getCodeNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getEngineTime()), TbStorage::getEngineTime, bo.getEngineTime());
        lqw.eq(StringUtils.isNotBlank(bo.getOutboundType()), TbStorage::getOutboundType, bo.getOutboundType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TbStorage::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getApprovedBy()), TbStorage::getApprovedBy, bo.getApprovedBy());
        lqw.eq(StringUtils.isNotBlank(bo.getReason()), TbStorage::getReason, bo.getReason());
        lqw.eq(bo.getSubmissionTime() != null, TbStorage::getSubmissionTime, bo.getSubmissionTime());
        lqw.eq(bo.getCreateTime() != null, TbStorage::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), TbStorage::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getUpdateTime() != null, TbStorage::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), TbStorage::getUpdateBy, bo.getUpdateBy());
        return lqw;
    }

    /**
     * 新增出入库申请
     */
    @Override
    public Boolean insertByBo(TbStorageBo bo) {
        TbStorage add = BeanUtil.toBean(bo, TbStorage.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改出入库申请
     */
    @Override
    public Boolean updateByBo(TbStorageBo bo) {
        TbStorage update = BeanUtil.toBean(bo, TbStorage.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbStorage entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除出入库申请
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
